#include <QDebug>
#include <QMessageBox>
#include "sliderscreen.h"

sliderscreen::sliderscreen(QWidget *parent) : QWidget(parent)
{
    //设置窗口大小
    this->setFixedSize(400,280);

    //创建滑块并初始化
    this->slider = new QSlider(Qt::Horizontal,this);    //水平方向滑块
    this->slider->setGeometry(-1,231,402,50);
    this->slider->setMaximum(500);
    this->slider->setPageStep(0);
    this->slider->setStyleSheet("QSlider::groove:horizontal{margin-top:0px;margin-bottom:-4px;border-image: url(../img/picture05.png);} \
                                 QSlider::handle:horizontal{width:60px; height:35px; border-image:url(../img/picture03.png);} \
                                 QSlider::sub-page:horizontal{border-image: url(../img/picture04.png);}");
    connect(this->slider,SIGNAL(sliderReleased()),this,SLOT(judgeSuccess()));

    //创建标签并插入图片
    this->label1 = new QLabel(this);
    this->label1->setGeometry(0,0,400,232);
    this->label1->setPixmap(QPixmap("../img/picture01.png"));
    this->label1->setScaledContents(true);
    this->label2 = new QLabel(this);
    this->label2->setGeometry(0,59,47,51);
    this->label2->setPixmap(QPixmap("../img/picture02.png"));
    this->label2->setScaledContents(true);

    //设置提示字体
    QFont text;
    text.setPointSize(12);
    text.setFamily("微软雅黑");
    this->label3 = new QLabel("拖动滑块验证",this);
    this->label3->setGeometry(140,216,160,80);
    this->label3->setFont(text);

    //创建定时器
    this->movetime = new QTimer();
    connect(this->movetime,SIGNAL(timeout()),this,SLOT(picMove()));
    this->movetime->start(0);
}

void sliderscreen::judgeSuccess()
{
    if(this->slider->value()<392 || this->slider->value()>398)
    {
        this->slider->setValue(0);
    }
    else
    {
        QMessageBox::information(nullptr,"温馨提示","验证成功!");
        exit(0);
    }
}

void sliderscreen::picMove()
{
    //qDebug() << this->slider->value();
    this->label2->move(this->slider->value()*0.7,this->label2->y());
}
